<template>
  <div class="app-container">
    <el-form
      label-position="right"
      label-width="80px"
      :rules="rules"
      :model="comment"
      ref="commentForm"
    >
      <el-form-item
        label="评论ID"
        prop="houseId"
      >
        <el-input
          v-model="comment.houseId"
          disabled
          style="width:100px"
        />
      </el-form-item>

      <el-form-item
        label="评论标题"
        prop="title"
      >
        <el-input
          v-model="comment.title"
          disabled
          style="width:350px"
        />
      </el-form-item>

      <el-form-item
        label="用户ID"
        prop="userId"
      >
        <el-input
          v-model="comment.userId"
          disabled
          style="width:100px"
        />
      </el-form-item>

      <el-form-item
        label="用户名"
        prop="userName"
      >
        <el-input
          v-model="comment.userName"
          disabled
          style="width:200px"
        />
      </el-form-item>

      <el-form-item
        label="评论内容"
        prop="commentContent"
      >
        <el-input
          type="textarea"
          :rows="3"
          v-model="comment.commentContent"
          style="width:350px"
        >
        </el-input>
      </el-form-item>

      <el-form-item
        label="评论时间"
        prop="commentDate"
      >        
        <el-date-picker
           v-model="comment.commentDate"
           type="datetime"
           placeholder="选择时间"
          value-format="yyyy-MM-dd HH:mm"
          format="yyyy-MM-dd HH:mm"
           :picker-options="pickerOptions"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          :disable="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate('commentForm')"
        >保存</el-button>
        <el-button @click="resetForm('commentForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import comment from '@/api/comment';
import user from '@/api/user';
import { getElement } from 'dropzone';

const defaultForm = {
  commentContent: '',
  commentDate: ''
};

export default {
  data() {
    return {
      comment: {
        houseId: '',
        title: '',
        userId: '',
        userName: '',
        commentContent: '',
        commentDate: ''
      },
      saveBtnDisabled: false, // 保存按钮是否禁用
      pickerOptions: {
        shortcuts: [
          {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          },
          {
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          },
          {
            text: '明天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }
        ]
      }
    };
  },
  // 监听
  watch: {
    // 当路由发生变化, 方法执行
    $route(to, from) {
      this.init();
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      // params表示路由中的参数, 指 path: 'edit/:id' 中类似:id的参数
      // 注意是route不是router
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id;
        this.fetchDataById(id);
      }
    },
    // 通过id获取项目
    fetchDataById(id) {
      comment
        .getCommentById(id)
        .then(response => {
          this.comment = response.data.comment;
        })
        .catch(response => {
          this.$message({
            type: 'error',
            message: '获取数据失败'
          });
        });
    },
    // 新建或修改
    saveOrUpdate(formName) {
      this.saveBtnDisabled = true;
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 如果comment对象中有commentId, 说明是修改, 因为创建不需要输入commentId
          // 这个comment对象是后端返回给前端的, 不是data中的
          this.updateData();
        } else {
          console.log('error submit');
          return false;
        }
      });
    },
    // 修改评论
    updateData() {
      this.saveBtnDisabled = true;
      comment
        .updateComment(this.comment)
        .then(response => {
          this.$message({
            type: 'success',
            message: '修改成功'
          });
        })
        .then(response => {
          this.$router.push({ path: '/comment' });
        })
        .catch(response => {
          this.$message({
            type: 'error',
            message: '保存失败'
          });
        });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style scoped>
span {
  color: #606266;
  margin-left: 12px;
  font-size: 14px;
  font-weight: 700;
}
</style>